var layer;
var form;
$(function(){
	layui.use(['layer','form'], function() {
		layer = layui.layer;
		form = layui.form;
		
		// 主题初选择 ==========================================================================================================
		selectTheme();

		// 初始化主题 ==========================================================================================================
		initThemeLike(ctxPath+"css/theme/"+theme+".css");
		// 初始化组件 ==========================================================================================================
		initThemeLike(ctxPath+"css/theme/module/"+moduleTheme+".css");
		
		// 是否显示标签页 ==========================================================================================================
		form.on('checkbox(page)', function (data) {
			var check = data.elem.checked;
			if(themeStyle){
			    var arr = themeStyle.split(',');
			    themeStyle = (check?"page1":"page0")+","+arr[1]+","+arr[2];
			}else{
				themeStyle = (check?"page1":"page0")+",refresh0,module0";
		    }
		    updateThemeStyle(themeStyle);
		});
		
		// 是否点击菜单刷新 ==========================================================================================================
		form.on('checkbox(refresh)', function (data) {
			var check = data.elem.checked;
			if(themeStyle){
				var arr = themeStyle.split(',');
				themeStyle = arr[0]+","+(check?"refresh1":"refresh0")+","+arr[2];
			}else{
		    	themeStyle = "page0,"+(check?"refresh1":"refresh0")+",module0";
		    }
		    updateThemeStyle(themeStyle);
		});
		
		// 是否组件跟随主题 ==========================================================================================================
		form.on('checkbox(module)', function (data) {
			var check = data.elem.checked;
			if(themeStyle){
				var arr = themeStyle.split(',');
				themeStyle = arr[0]+","+arr[1]+","+(check?"module1":"module0");
			}else{
		    	themeStyle = "page0,refresh0,"+(check?"module1":"module0");
		    }
		    updateThemeStyle(themeStyle);
		});
		
	});
	
	//关闭主题 ==========================================================================================================
	$(".close_theme").click(function(){
		$(this).fadeOut();
		$("#theme_div").css("right","-300px");
	})


})


/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 添加like
 * @param src
 */
function initThemeLike(src) {
	var link = document.createElement("link");
	link.rel = "stylesheet";
	link.type = "text/css";
	link.href = src;
	document.getElementsByTagName("head")[0].appendChild(link);
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 打开主题
 * @returns
 */ 
function settingTheme(){
	$(".close_theme").css("display","inline-block");
	$("#theme_div").css("right","0px");
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 主题更改
 */
function includeLinkStyle(url, $this) {
	if(theme == url){
		return layer.msg("当前主题");
	}
	var lay = layer.load(0,{
		shade: [0.1, 'gray']
	});
	var list = document.getElementsByTagName('link');
	if (list.length > 0) {
		for (var i = list.length - 1; i >= 0; i--) {// 删除之前的主题
			var h = list[i].getAttribute('href');
			if (h && h.indexOf('/theme') != -1) {
				list[i].parentNode.removeChild(list[i]);
			}
		}
	}
	if (url) {//重新赋予新的主题
		var link = document.createElement("link");
		link.rel = "stylesheet";
		link.type = "text/css";
		link.href = 'css/theme/' + url + '.css';
		document.getElementsByTagName("head")[0].appendChild(link);
		if(themeStyle.indexOf('module1') != -1){
			var link = document.createElement("link");
			link.rel = "stylesheet";
			link.type = "text/css";
			link.href = 'css/theme/module/module_' + url + '.css';
			document.getElementsByTagName("head")[0].appendChild(link);
		}
		theme = url;
		$(".theme_li_this").removeClass("theme_li_this");
		$($this).addClass("theme_li_this");
	}
	$.post(ctxPath+'main/theme',{'theme' : url},function(res){
		layer.close(lay);
		layer.msg(res.msg);
	})
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 主题样式更改
 */
function updateThemeStyle(value) {
	var lay = layer.load(0,{
		shade: [0.1, 'gray']
	});
	$.post(ctxPath+'main/theme',{'themeStyle' : value},function(res){
		layer.close(lay);
		if(res.success){
			document.location.reload();//重新加载当前页面
		}
		layer.msg(res.msg);
	})
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 主题选择
 */
function selectTheme(){
	var arr1 = ['#23262E','#23262E','#23262E','#e02e1a','#f7a209','#009688','#4a96ad','#594aad',
		'#0e1015','#1b91f7','#1b91f7','#1aa094','#f7a209','#9a230b','#271f56','#3198f1','#1aa094','#f7a209','#9a230b','#271f56','#ffa4d1'];
	var arr2 = ['#ffffff','#ffffff','#ffffff','#ffffff','#ffffff','#ffffff','#ffffff','#ffffff',
		'#0e1015','#0e1015','#1b91f7','#1aa094','#f7a209','#9a230b','#271f56','#3198f1','#1aa094','#f7a209','#9a230b','#271f56','#ffa4d1'];
	var arr3 = ['#23262E','#23262E','#23262E','#e02e1a','#f7a209','#009688','#4a96ad','#594aad',
		'#ffffff','#ffffff','#ffffff','#ffffff','#ffffff','#ffffff','#ffffff','#0e1015','#0e1015','#0e1015','#0e1015','#0e1015','#0e1015'];
	var s = '';
	for(var i=0; i<arr1.length; i++){
		if(theme == ('theme'+(i+1))){
			s += '<li class="theme_li_this" onclick="includeLinkStyle(\'theme'+(i+1)+'\', this)">';
		}else{
			s += '<li onclick="includeLinkStyle(\'theme'+(i+1)+'\', this)">';
		}
		s += '	<div>';
		s += '		<span style="display: block; width: 20%; float: left; height: 15px; background: '+arr1[i]+';"></span>';
		s += '		<span style="display: block; width: 80%; float: left; height: 15px; background: '+arr2[i]+';"></span>';
		s += '	</div>';
		s += '	<div>';
		s += '		<span style="display: block; width: 20%; float: left; height: 45px; background: '+arr3[i]+';"></span>';
		s += '		<span style="display: block; width: 80%; float: left; height: 45px; background: #ffffff;"></span>';
		s += '	</div>';
		s += '</li>';
	}
	$('#theme_ul').html(s);
	s = ''; 
	var check1 = '';
	if(themeStyle.indexOf("page1") != -1){
		check1 = 'checked';
	}
	s += '<input '+check1+' type="checkbox" name="page" lay-filter="page" title="标签页">';
	var check2 = '';
	if(themeStyle.indexOf("refresh1") != -1){
		check2 = 'checked';
	}
	s += '<input '+check2+' type="checkbox" name="refresh" lay-filter="refresh" title="点击菜单刷新">';
	var check3 = '';
	if(themeStyle.indexOf("module1") != -1){
		check3 = 'checked';
	}
	s += '<input '+check3+' type="checkbox" name="module" lay-filter="module" title="组件颜色跟随主题">';
	$('#theme_tab').append(s);
	form.render("checkbox")
}



